# -*- coding: utf-8 -*-
import logging
import subprocess

from mon.baselib.utils import StringUtils

from src.handler.AbstractHandler import AbstractHandler

browser_dict = {
    "baidu": "https://www.baidu.com",
    "elkp": "http://152.136.142.244:32076/login",
    "elk": "http://81.70.61.57:5601/login",

    "webp": "https://webpre.bazhongba.com/login",
    "web": "https://web.bazhongba.com/login",

    "andp": "https://www.pgyer.com/bazhongba_pre_Android",
    "and": "https://www.pgyer.com/bazhongba_release_Android",

    "iosp": "https://www.pgyer.com/bazhongba_pre_ios",
    "ios": "https://www.pgyer.com/bazhongba_zs_ios",
    "tx": "https://docs.qq.com/sheet/DYmdsQXVtU2tCZUNF?nlc=1&tab=uba9qp",
}

browser_password = {
    "webp": "1qaz@WSX.",
    "web": "2qaz@Mon",
}

chrome_path = 'C:/Users/Administrator/AppData/Local/Google/Chrome/Application/chrome.exe'


def print_password(is_log: str, name: str):
    if StringUtils.str_to_bool(is_log):
        pwd = browser_password.get(name)
        if StringUtils.not_empty(pwd):
            logging.info(f" Password: {pwd}")


def open_new_tab(url):
    logging.info(f"Open new tab: {url}")
    subprocess.Popen([chrome_path, url], shell=True)


class BrowserOpenHandler(AbstractHandler):
    def _setup_arg(self, subparsers):
        parser_browser_open = subparsers.add_parser('bo', help='打开浏览器')
        parser_browser_open.add_argument('--tab', '-t', type=bool, default=True, help='open by new tab')
        parser_browser_open.add_argument('--name', '-n', type=str, default='', help='open url name')
        parser_browser_open.add_argument('--password', '-p', type=str, default='True', help='enable logging password')
        parser_browser_open.add_argument('--url', '-u', type=str, default='', help='open url name')
        parser_browser_open.set_defaults(func=self.do_process)

    def _check(self, args):
        """检查参数是否有效"""
        return True

    def _process(self, args):

        if StringUtils.not_empty(args.url):
            open_new_tab(args.url)
            return

        if StringUtils.is_empty(args.name):
            logging.info("open name is empty")
            return

        url = browser_dict[args.name]
        if StringUtils.is_empty(url):
            logging.info("open url not found")
            return
        print_password(args.password, args.name)
        open_new_tab(url)
